package com.campus.dao.impl;

import com.campus.dao.CourseDao;
import com.campus.dto.CourseConditionDto;
import com.campus.model.Class;
import com.campus.model.Course;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class CourseDaoImpl implements CourseDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int createCourse(Course course) {
        String sql = "INSERT INTO t_course (course_name, created_time, status, activity_id, teacher_id, class_id) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        try {
            return jdbcTemplate.update(sql,
                    course.getCourseName(),
                    new java.sql.Timestamp(System.currentTimeMillis()),  // 设置当前时间作为创建时间
                    course.getStatus(),
                    course.getActivityId(),
                    course.getTeacherId(),
                    course.getClassId());
        } catch (Exception e) {
            log.error("创建课程时出错: {}", e.getMessage(), e);
        }
        return 0; // 插入失败返回 0
    }

    @Override
    public List<Course> getCoursesByCondition(CourseConditionDto condition) {
        StringBuilder sql = new StringBuilder("SELECT * FROM t_course WHERE 1=1");
        List<Object> params = new ArrayList<>();

        // 动态拼接查询条件
        if (condition.getCourseName() != null && !condition.getCourseName().isEmpty()) {
            sql.append(" AND course_name LIKE ?");
            params.add("%" + condition.getCourseName() + "%");
        }
        if (condition.getStatus() != null && !condition.getStatus().isEmpty()) {
            sql.append(" AND status = ?");
            params.add(condition.getStatus());
        }
        if (condition.getActivityId() != null) {
            sql.append(" AND activity_id = ?");
            params.add(condition.getActivityId());
        }
        if (condition.getTeacherId() != null) {
            sql.append(" AND teacher_id = ?");
            params.add(condition.getTeacherId());
        }
        if (condition.getClassId() != null) {
            sql.append(" AND class_id = ?");
            params.add(condition.getClassId());
        }

        return jdbcTemplate.query(sql.toString(), params.toArray(), new RowMapper<Course>() {
            @Override
            public Course mapRow(ResultSet rs, int rowNum) throws SQLException {
                Course course = new Course();
                course.setId(rs.getInt("id"));                       // 假设有 `id` 字段
                course.setCourseName(rs.getString("course_name"));   // 映射 `course_name`
                course.setCreatedTime(rs.getDate("created_time"));
                course.setStatus(rs.getString("status"));           // 映射 `status`
                course.setActivityId(rs.getInt("activity_id"));      // 映射 `activity_id`
                course.setTeacherId(rs.getInt("teacher_id"));        // 映射 `teacher_id`
                course.setClassId(rs.getInt("class_id"));            // 映射 `class_id`
                return course;
            }
        });
    }

    @Override
    public int deleteCourse(int courseId) {
        String sql = "DELETE FROM t_course WHERE id = ?";
        try {
            return jdbcTemplate.update(sql, courseId);
        } catch (Exception e) {
            log.error("删除课程时出错: {}", e.getMessage(), e);
        }
        return 0; // 删除失败返回 0
    }

    @Override
    public int updateCourse(Course course) {
        String sql = "UPDATE t_course SET course_name = ?, status = ?, activity_id = ?, teacher_id = ?, class_id = ? " +
                "WHERE id = ?";
        try {
            return jdbcTemplate.update(sql,
                    course.getCourseName(),
                    course.getStatus(),
                    course.getActivityId(),
                    course.getTeacherId(),
                    course.getClassId(),
                    course.getId());
        } catch (Exception e) {
            log.error("编辑课程时出错: {}", e.getMessage(), e);
        }
        return 0; // 更新失败返回 0
    }

    @Override
    public List<Course> findAllCourses() {
        String sql = "SELECT * FROM t_course";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Course.class));
    }

}
